﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MySql.Data.MySqlClient;
using DevExpress.XtraTab;
using System.Data.SqlClient;

namespace DMU.Gui
{
    public partial class ucHoangHoa : DevExpress.XtraEditors.XtraUserControl
    {
        private XtraTabControl tab;
        public ucHoangHoa(XtraTabControl tab)
        {

            InitializeComponent();
            this.tab = tab;

        }
        public  DataTable dtSV;
        public bool them,findToTen=true;

        Service sv = new Service();
        help hp = new help();

        private void refreshData(DataTable dt)
        {
            gridControl1.DataSource = dt;
        }

        private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.IsGetData)
                e.Value= e.ListSourceRowIndex +1;
        }

        private void ucSinhVien_Load(object sender, EventArgs e)
        {
            dtSV = new DataTable();
            DataColumn[] cl = new DataColumn[9];
            cl[0] = new DataColumn("MSSV", Type.GetType("System.String"));
            cl[1] = new DataColumn("Holot", Type.GetType("System.String"));
            cl[2] = new DataColumn("Ten", Type.GetType("System.String"));
            cl[3] = new DataColumn("NgaySinh", Type.GetType("System.String"));
            cl[4] = new DataColumn("NoiSinh", Type.GetType("System.String"));
            cl[5] = new DataColumn("GioiTinh", Type.GetType("System.Boolean"));
            cl[6] = new DataColumn("KhoaHoc", Type.GetType("System.String"));
            cl[7] = new DataColumn("MaLop", Type.GetType("System.String"));
            cl[8] = new DataColumn("IsActive", Type.GetType("System.Boolean"));

            dtSV.Columns.AddRange(cl);
            DataColumn[] c = new DataColumn[1];
            c[0] = dtSV.Columns["MSSV"];
            dtSV.PrimaryKey = c;

            gridControl1.DataSource = dtSV;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            them = true;
            GuiPro.frmThemHangHoa frm = new GuiPro.frmThemHangHoa(this);
            frm.ShowDialog();
        }


        private void btnSua_Click(object sender, EventArgs e)
        {
            int rowHandle=gridView1.FocusedRowHandle;
            if ( rowHandle>= 0 )
            {
                them = false;
                GuiPro.frmThemHangHoa frm = new GuiPro.frmThemHangHoa(this);
                frm.Text = "Bảng sửu đổi thông tin";

          /*      frm.txtMa.Text = gridView1.GetRowCellValue(rowHandle, "MSSV").ToString();
                frm.txtHo.Text = gridView1.GetRowCellValue(rowHandle, "Holot").ToString();
                frm.txtTen.Text = gridView1.GetRowCellValue(rowHandle, "Ten").ToString();
                frm.dteNgayS.Text = gridView1.GetRowCellValue(rowHandle, "NgaySinh").ToString();
                frm.txtNoiSinh.Text = gridView1.GetRowCellValue(rowHandle, "NoiSinh").ToString();
                frm.chkNam.Checked = bool.Parse(gridView1.GetRowCellValue(rowHandle, "GioiTinh").ToString());
                frm.txtKH.Text = gridView1.GetRowCellValue(rowHandle, "KhoaHoc").ToString();
                frm.lkpLop.Text = gridView1.GetRowCellValue(rowHandle, "MaLop").ToString();
                frm.chkActive.Checked = bool.Parse(gridView1.GetRowCellValue(rowHandle, "IsActive").ToString());
*/
                frm.ShowDialog();
            }
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            if (txtFind.Text.Trim() == "")
            {
                txtFind.Focus();
                return;
            }

            if (findToTen)
                dtSV = sv.getDatatable("dmu_proSFindToTenSV", new SqlParameter("Ten0", txtFind.Text + "%"));
            else
                dtSV = sv.getDatatable("dmu_proSVFindToLop", new SqlParameter("TenLop0", txtFind.Text + "%"));

            gridControl1.DataSource = dtSV;
        }

        private void txtFind_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                EventArgs et = new EventArgs();
                btnFind_Click(sender, et);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int[] rowSelec = gridView1.GetSelectedRows(); int dem = 0; int len = rowSelec.Length;

            for (int i = 0; i < len; i++)
            {
                if (rowSelec[i] < 0)
                {
                    gridView1.UnselectRow(rowSelec[i]);
                }
                else
                    break;
            }

            rowSelec = gridView1.GetSelectedRows(); len = rowSelec.Length;

            if (len> 0)
            {
                try
                {
                    if (hp.showMes(len + " đối tượng sẽ bị xóa ?", 2) == DialogResult.Yes)
                    {
                        sv.openConnect();
                        for (int i = 0; i < len; i++)
                        {
                            if (gridView1.GetRowCellValue(rowSelec[i], "MSSV") != null)
                            {
                                bool t=sv.cusTomExecuteNonqueryStore("dmu_proSXoaSV", new SqlParameter("MSSV0", gridView1.GetRowCellValue(rowSelec[i], "MSSV").ToString()));
                                
                                if (!t)
                                {
                                    gridView1.UnselectRow(rowSelec[i]);
                                    dem++;
                                }
                            }
                        }
                        sv.closeConnect();
                        gridView1.DeleteSelectedRows();
                        if (dem > 0)
                            hp.showMes(dem + " đối tượng không thể xóa !", 1);
                        else
                            hp.showMes("Xóa thành công ", -1);
                    }
                }
                catch(Exception et)
                {
                    hp.showMes("Có lỗi khi xóa. Vui lòng thử lại ! " + et.Message, 1);
                }
            }
            else
            {
                hp.showMes("Chọn dòng để thực hiện chức năng này !!", -1);
            }
                
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            tab.TabPages.RemoveAt(tab.SelectedTabPageIndex);
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                EventArgs et = new EventArgs();
                btnSua_Click(sender, et);
            }
        }

        private void gridControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                EventArgs et = new EventArgs();
                btnXoa_Click(sender, et);
            }
        }

        private void chkTheoTen_CheckedChanged_1(object sender, EventArgs e)
        {
          //  if (!chkTheoTen.Checked)
           // {
           //     findToTen = false;

          //  }
          //  else
             //   findToTen = true;

           // btnFind_Click(sender, e);
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                EventArgs et = new EventArgs();
                btnSua_Click(sender, et);
            }
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }



    }
}
